package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.HintEnums;
import com.ytjj.qmyx.admin.model.Dept;
import com.ytjj.qmyx.admin.model.Role;
import com.ytjj.qmyx.admin.model.request.DeptReq;
import com.ytjj.qmyx.admin.service.DeptService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/dept")
public class DeptController {

    private DeptService deptService;

    @PostMapping("/deptList")
    @OperLog(operModule = "部门模块", operDesc = "部门列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectList(@RequestBody Dept dept) {
        return CommonResult.success(deptService.deptList(dept));
    }

    @PostMapping("/deptLists")
    @OperLog(operModule = "部门模块", operDesc = "部门详情列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult deptLists(@RequestBody DeptReq deptReq) {
        return CommonResult.success(deptService.deptLists(deptReq));
    }

    @GetMapping(value = "/insertDept")
    @OperLog(operModule = "部门模块", operDesc = "新增部门", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertDept(String deptName){
        if (deptService.findByDeptName(deptName)>0){
            return CommonResult.failed(HintEnums.DEPT_NAME_IS_REPETITION.getMessage());
        }
        return CommonResult.success(deptService.insertDept(deptName));
    }

    @PostMapping(value = "/updateDept")
    @OperLog(operModule = "部门模块", operDesc = "修改部门", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateDept(@RequestBody Dept dept){
        if (deptService.findByDeptName(dept.getDeptName())>0){
            return CommonResult.failed(HintEnums.DEPT_NAME_IS_REPETITION.getMessage());
        }
        return CommonResult.success(deptService.updateDept(dept));
    }

    @GetMapping(value = "/selectDept")
    @OperLog(operModule = "部门模块", operDesc = "按部门ID查询部门信息", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectDept(Integer id){
        return CommonResult.success(deptService.selectDept(id));
    }

    @GetMapping(value = "/roleList")
    @OperLog(operModule = "部门模块", operDesc = "按部门ID查询角色信息", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult roleList(Integer id){
        return CommonResult.success(deptService.roleList(id));
    }

    @GetMapping(value = "/deleteDept")
    @OperLog(operModule = "部门模块", operDesc = "移除部门", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult deleteDept(Integer id){
        if(deptService.adminList(id)>0){
            return CommonResult.failed(HintEnums.DEPT_ADMIN_IS_NULL.getMessage());
        }
        return CommonResult.success(deptService.deleteDept(id));
    }

    /**
     * 根据部门id查询角色
     * @param
     * @return
     */
    @PostMapping(value = "/findRoleByDeptId")
    public CommonResult findRoleByDeptId(@RequestBody Dept dept) {
        List<Role> roleByDeptId = deptService.findRoleByDeptId(dept.getId());
        if (roleByDeptId == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(roleByDeptId);
    }

    /**
     * 根据adminid修改角色id
     * @param roleId
     * @param adminId
     * @return
     */
    @GetMapping("/updateRoleByAdminId")
    public CommonResult updateRoleByAdminId(@RequestParam Integer roleId,@RequestParam Integer adminId) {
        return CommonResult.success(deptService.updateRoleByAdminId(roleId,adminId));
    }
}
